from flask import render_template, g, request, jsonify, current_app
from werkzeug.utils import redirect

from info import constants
from info.models import News
from info.utils.common import user_login_data
from info.utils.image_storege import storage
from info.utils.response_code import RET
from . import profile_blu

@profile_blu.route('/base_info',methods=["POST","GET"])
@user_login_data
def base_info():
    user = g.user
    if request.method== "GET":
        return render_template('user_base_info.html',data={'user':user.to_dict()})

    nick_name = request.json.get('nick_name')
    signature = request.json.get('signature')
    gender = request.json.get('gender')
    if not all([nick_name,signature,gender]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")
    if gender not in ["WOMAN","MAN"]:
        return jsonify(errno=RET.PARAMERR, errmsg="查询错误")
    user.nick_name = nick_name
    user.signature = signature
    user.gender = gender

    return jsonify(errno=RET.OK, errmsg="成功")


@profile_blu.route('/pic_info',methods=["POST","GET"])
@user_login_data
def pic_info():
    user = g.user
    if request.method =="GET":
        return render_template('news/user_pic_info.html',data={'user':user.to_dict()})
    avatar = request.files.get('avatar').read()
    try:
        key = storage(avatar)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="查询错误")
    user.avatar_url = key
    return jsonify(errno=RET.OK, errmsg="成功",data={'avatar_url':constants.QINIU_DOMIN_PREFIX+key})



@profile_blu.route('/pass_info',methods=["POST","GET"])
@user_login_data
def pass_info():
    user = g.user
    if request.method =="GET":
        return render_template('news/user_pass_info.html',data={'user':user.to_dict()})
    old_password = request.json.get("old_password")
    new_password = request.json.get("new_password")
    if not all([old_password,new_password]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")
    try:
        old_password = int(old_password)
        new_password = int(new_password)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")
    if not user.check_passowrd(old_password):
        return jsonify(errno=RET.PARAMERR, errmsg="密码错误")
    user.password = new_password
    return jsonify(errno=RET.OK, errmsg="成功")


@profile_blu.route('/collection')
@user_login_data
def news_collection():
    page = request.args.get('p',1)
    per_page =10
    try:
        page = int(page)
    except Exception as e:
        current_app.logger.error(e)
        page = 1
    user = g.user
    news_list = []
    current_page = 1
    total_page = 1
    items = []
    if user.collection_news:
        try:
            paginate = user.collection_news.paginate(page,per_page,False)
            current_page = paginate.page
            total_page = paginate.pages
            items = paginate.items
        except Exception as e :
            current_app.logger.error(e)
    for news in items:
        news_list.append(news.to_dict())
    data={
        "collections":news_list,
        "total_page":total_page,
        'current_page':current_page
    }
    return jsonify(errno=RET.OK, errmsg="成功",data=data)


@profile_blu.route('/info')
@user_login_data
def user_info():
    user = g.user
    if not user :
        return redirect('/')
    return render_template('news/user.html',data=user.to_dict())